<?php

namespace app\api\controller\european;

use app\api\controller\Controller;
use app\common\model\european\EuropeanActivity;
use app\common\model\european\EuropeanChampion;
use app\common\model\european\EuropeanCountry;
use app\common\model\european\EuropeanMatch;
use app\common\model\european\EuropeanScore;
use app\common\model\european\EuropeanUserPicking;

use app\common\model\order\Order;
use app\Request;

class European extends Controller
{
    private $user;

    public function initialize()
    {
        $this->user = $this->getUser();

    }


    /**
     *
     * getEuropeanActivity
     * void
     * @return void
     * @Author kevin
     * @date 2024/6/6 14:46
     */
    public function getEuropeanActivity(){
        $data = EuropeanActivity::find();
        return $this->renderSuccess('', compact('data'));
    }
    /**
     *参赛国家列表
     * getCountryList
     * \think\response\Json
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author kevin
     * @date 2024/6/1 17:39
     */
   public function getCountryList()
   {
       $map = [];
       $user_id = $this->user['user_id'];
       $list = EuropeanCountry::where($map) -> select();

       $european_champion = EuropeanChampion::where('user_id',$user_id) -> find();

       $european_activity = EuropeanActivity::find();
       $result_european_country_id = $european_activity['european_country_id'];

       $champion['country'] = '待公布';
       $champion['national_flag'] = '';
       $choose_data = null;
       foreach ($list as $key => $value){
           $list[$key]['is_choose'] = 0;
           //冠军
           if($result_european_country_id == $value['european_country_id']){
                $champion['country'] = $value['country'];
                $champion['national_flag'] = $value['national_flag'];
           }
           if(isset($european_champion['european_country_id']) &&  $european_champion['european_country_id'] == $value['european_country_id']){
               $list[$key]['is_choose'] = 1;
               $choose_data = $value;

           }
       }
       $bg_img = $european_activity['bg_img'];
       $match['start_time'] = date('Y-m-d H:i:s',$european_activity['start_time']);
       $match['end_time'] = date('Y-m-d H:i:s',$european_activity['end_time']);
       return $this->renderSuccess('', compact('list', 'choose_data','champion','bg_img','match'));

   }


    /**
     *比分列表
     * getEuropeanScoreList
     * \think\response\Json
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author kevin
     * @date 2024/6/1 17:40
     */
    public function getEuropeanScoreList()
    {
        $map = [];
        $list = EuropeanScore::where($map) -> select();
        return $this->renderSuccess('', compact('list'));

    }


    /**
     *赛事列表
     * getEuropeanMatchList
     * \think\response\Json
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author kevin
     * @date 2024/6/1 17:42
     */
    public function getEuropeanMatchList()
    {
        $map = [];
        $list = EuropeanMatch::where($map) -> order('start_time asc') -> select();
        $match_data = null;
        foreach ($list as $key => $value){

            $list[$key]['start_time_text'] = date('Y-m-d H:i:s',$value['start_time']);
            $list[$key]['end_time_text'] = date('Y-m-d H:i:s',$value['end_time']);
            if($value['start_time'] <= time() && $value['end_time'] > time()){
                $list[$key]['status'] = 1;
                $list[$key]['status_text'] = '进行中';
                $match_data = $value;
            }elseif($value['end_time'] < time()){
                $list[$key]['status'] = 2;
                $list[$key]['status_text'] = '已结束';
                $match_data = $value;
            }else{
                $list[$key]['status'] = 0;
                $list[$key]['status_text'] = '未开始';
            }
        }

        return $this->renderSuccess('', compact('match_data','list'));

    }

    /**
     *竞猜列表
     * getPickingList
     * \think\response\Json
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author kevin
     * @date 2024/6/1 18:04
     */
    public function getPickingList()
    {

        $map = [];
        $european_score = EuropeanScore::column('score','european_score_id');
        $list = EuropeanUserPicking::where($map) -> select();
        foreach ($list as $key => $value){
            $list[$key]['score_text'] = $european_score[$value['picking_score_id']]??'';
            $list[$key]['result_score_text'] = $european_score[$value['result_picking_score_id']]??'待公布';
        }
        return $this->renderSuccess('', compact('list'));
    }

    /**
     *用户竞猜数据
     * getUserPickingData
     * \think\response\Json
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author kevin
     * @date 2024/6/1 18:06
     */
    public function getUserPickingData()
    {
        $params = $this -> postData();
        $order_no = $params['order_no']??'';

        if(!$order_no){
            return $this->renderError('订单编号不能为空');
        }
        $map = [];
        $user_id = $this->user['user_id'];
        $map[] = ['user_id','=',$user_id];
        $map[] = ['order_no','=',$order_no];

        $european_score = EuropeanScore::column('score','european_score_id');
        $user_picking = EuropeanUserPicking::where($map) -> find();
        $european_match = EuropeanMatch::column('team,national_flag_l,national_flag_r,start_time,end_time,bg_img,bg_color','european_match_id');

        if($user_picking){
            $user_picking['score_text'] = $european_score[$user_picking['european_score_id']]??'';
            $user_picking['result_score_text'] = $european_score[$user_picking['result_european_score_id']]??'待公布';
            $user_picking['team'] = $european_match[$user_picking['european_match_id']]['team']??'';
            $user_picking['national_flag_l'] = $european_match[$user_picking['european_match_id']]['national_flag_l']??'';
            $user_picking['national_flag_r'] = $european_match[$user_picking['european_match_id']]['national_flag_r']??'';

            $user_picking['bg_img'] = $european_match[$user_picking['european_match_id']]['bg_img']??'';
            $user_picking['bg_color'] = $european_match[$user_picking['european_match_id']]['bg_color']??'';

            $start_time = $european_match[$user_picking['european_match_id']]['start_time']??'';
            $end_time = $european_match[$user_picking['european_match_id']]['end_time']??'';

            $user_picking['start_time_text'] = date('Y-m-d H:i:s',$start_time);
            $user_picking['end_time_text'] = date('Y-m-d H:i:s',$end_time);

            $user_picking['is_prize_text'] = '进行中';
            if($user_picking['is_prize'] == 1){
                $user_picking['is_prize_text'] = '已猜中';
            }elseif($user_picking['is_prize'] == 2){
                $user_picking['is_prize_text'] = '未猜中';
            }else{
                $user_picking['is_prize_text'] = '进行中';
            }
            $user_picking['is_give_text'] = '';
            if($user_picking['is_prize'] == 1){
                if($user_picking['is_give'] == 1){
                    $user_picking['is_give_text'] = '已发放';
                }else{
                    $user_picking['is_give_text'] = '待发放';
                }
            }
        }

        return $this->renderSuccess('', compact('user_picking'));
    }


    /**
     *竞猜冠军
     * guessingChampion
     * \think\response\Json
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\DbException
     * @Author kevin
     * @date 2024/6/3 11:12
     */
    public function guessingChampion()
    {
        $params = $this -> postData();
        $user_id = $this->user['user_id'];
        $params['user_id'] = $user_id;
        $params['nickName'] = $this->user['nickName'];
        $params['mobile'] = $this->user['mobile'];

        if(!EuropeanCountry::where('european_country_id',$params['european_country_id']) -> count()){
            return $this->renderError('数据不存在');
        }

        $european_activity = EuropeanActivity::find();
        //6月10 - 6月13
        $start_time = $european_activity['start_time'];
        $end_time = $european_activity['end_time'];

        if($start_time > time()){
            return $this->renderError('竞猜还没开始');
        }
        if($end_time < time()){
            return $this->renderError('竞猜已结束');
        }

        if(EuropeanChampion::where('user_id',$user_id) -> count()){
            return $this->renderError('您参与竞猜，请勿重复提交');
        }
        if(EuropeanChampion::createData($params)){
            return $this->renderSuccess('竞猜成功');
        }else{
            return $this->renderError('提交失败');
        }

    }

    /**
     *比分竞猜
     * guessingScore
     * \think\response\Json
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author kevin
     * @date 2024/6/3 15:04
     */
    public function guessingScore()
    {
        $params = $this -> postData();
        $user_id = $this->user['user_id'];
        $params['user_id'] = $user_id;
        $params['nickName'] = $this->user['nickName'];
        $params['mobile'] = $this->user['mobile'];

        $european_match_id = $params['european_match_id'];//赛事id
        $european_score_id = $params['european_score_id'];//比分id

        if(!$params['order_no']){
            return $this->renderError('订单号不能为空');
        }

        $order_data = Order::where('order_no',$params['order_no']) -> find();

        if(!$order_data){
            return $this->renderError('订单不能不存在');
        }

        $picking_num = $order_data['picking_num'];
        if(!$picking_num || $picking_num<1){
            return $this->renderError('您没有竞猜次数');
        }


        if(!EuropeanScore::where('european_score_id',$european_score_id) -> count()){
            return $this->renderError('比分不存在');
        }

        $params['shop_supplier_id'] = $order_data['shop_supplier_id'];

        $european_match = EuropeanMatch::where('european_match_id',$european_match_id) -> find();

        if(!$european_match){
            return $this->renderError('赛事不存在');
        }

        if($european_match['start_time'] > time()){
            return $this->renderError('竞猜还没开始');
        }
        if($european_match['end_time'] < time()){
            return $this->renderError('竞猜已结束');
        }

        $map = [];
        $map[] = ['user_id','=',$user_id];
        $map[] = ['european_match_id','=',$european_match['european_match_id']];
        $map[] = ['is_delete','=',0];
        //同一个场次只要有次数就可以竞猜
//        if(EuropeanUserPicking::where($map) -> count()){
//            return $this->renderError('您参与竞猜，请勿重复提交');
//        }
        if(EuropeanUserPicking::createData($params)){
            Order::where('order_no',$params['order_no']) -> update(['is_picking' => 1,'picking_num' => 0]);
            return $this->renderSuccess('竞猜成功');
        }else{
            return $this->renderError('提交失败');
        }
    }

    /**
     *我的竞猜列表
     * getMyGuessingScoreList
     * void
     * @return void
     * @Author kevin
     * @date 2024/6/3 14:27
     */
    public function getMyGuessingScoreList()
    {
        $user_id = $this->user['user_id'];
        $params =  $this -> postData();
        $european_score = EuropeanScore::column('score','european_score_id');
        $european_match = EuropeanMatch::column('team,national_flag_l,national_flag_r','european_match_id');
        $list = EuropeanUserPicking::where('user_id',$user_id) -> select();
        foreach ($list as $key => $value){
            $list[$key]['score_text'] = $european_score[$value['european_score_id']]??'';
            $list[$key]['result_score_text'] = $european_score[$value['result_european_score_id']]??'待公布';
            $list[$key]['team'] = $european_match[$value['european_match_id']]['team']??'';
            $list[$key]['national_flag_l'] = $european_match[$value['european_match_id']]['national_flag_l']??'';
            $list[$key]['national_flag_r'] = $european_match[$value['european_match_id']]['national_flag_r']??'';
            $list[$key]['is_prize_text'] = '待公布';
            if($value['is_prize'] == 1){
                $list[$key]['is_prize_text'] = '已猜中';
            }elseif($value['is_prize'] == 2){
                $list[$key]['is_prize_text'] = '未猜中';
            }
            $list[$key]['is_give_text'] = '';
            if($value['is_prize'] == 1){
                if($value['is_give'] == 1){
                    $list[$key]['is_give_text'] = '已发放';
                }else{
                    $list[$key]['is_give_text'] = '待发放';
                }
            }
        }
        return $this->renderSuccess('我的竞猜列表', compact('list'));
    }


}